package com.hyq.market.ui.home.home;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyq.market.R;
import com.hyq.market.adapter.HotGoodsAdapter;
import com.hyq.market.adapter.IndexClassAdapter;
import com.hyq.market.api.BaseRequest;
import com.hyq.market.base.IMode;
import com.hyq.market.bean.ClassIndex;
import com.hyq.market.bean.Goods;
import com.hyq.market.ui.goodsDetail.GoodsDetail;
import com.hyq.market.ui.home.PersenterAdapterListener;
import com.hyq.market.ui.home.model.IndexUtil;
import com.hyq.market.ui.home.model.ModelGoods;
import com.hyq.market.ui.home.model.ModelLike;
import com.hyq.market.ui.search.SearchAct;

/**
 * Created by Administrator on 2017/7/19 0019.
 */

public class PersenterHome  extends PersenterAdapterListener<IViewHome> {
    private IViewHome iView;
    private IMode iMode;
    private boolean pullMore;
    private int totalPages;
    private int nowPage=1;
    private int totalPagesGoods;

    public PersenterHome(IViewHome iView) {
        this.iView=iView;
    }


    @Override
    public <T> void getData(T t) {
        getData();
    }

    public  void getData() {
        pullMore=false;
        BaseRequest request=new BaseRequest();
        getDataBanner(request);
        getDataClass(request);
        getDataGoods(request);
    }

    public  void getDataBanner(BaseRequest request){

    }
    public  void getDataClass(BaseRequest request){
            iMode=new IndexUtil();
            iMode.getData(this);
    }


    public void getDataGoods(BaseRequest request){
        iMode=new ModelGoods();
        iMode.getData(request,this);
    }


    private void loadDataClass(ClassIndex classIndex){
        if (pullMore)
        {
            iView.pullMoreComlite();
        }else {
            iView.getAdapter(1).getData().clear();
            iView.refreshComplite();
        }
        iView.getAdapter(1).getData().addAll(classIndex.getData().getGoods_category());
        iView.getAdapter(1).notifyDataSetChanged();
    }

    private void loadDataGoods(Goods goods){
        if (pullMore)
        {
            iView.pullMoreComlite();
        }else {
            iView.getAdapter(2).getData().clear();
            iView.refreshComplite();
        }
        iView.getAdapter(2).getData().addAll(goods.getData());
        iView.getAdapter(2).notifyDataSetChanged();
        totalPagesGoods=goods.getPage().getTotalPages();
        nowPage=goods.getPage().getNowPage();
    }

    @Override
    public <T> void netSucceed(T t, int code) {
        if(t instanceof ClassIndex)
        {
            loadDataClass((ClassIndex) t);
            return;
        }

        if (t instanceof Goods)
        {
            loadDataGoods((Goods) t);
            return;
        }
        iView.refreshNoData();

    }



    @Override
    public void onRefresh() {
       getData(null);
    }

    @Override
    public void onLoadMoreRequested() {
        if (nowPage>=totalPagesGoods)
        {
            iView.pullMoreNoMore();
            return;
        }

        BaseRequest request=new BaseRequest();
        request.setPageNo(nowPage+1);
        pullMore=true;
        getDataGoods(request);
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
       onItemChildClick(adapter,view,position);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof IndexClassAdapter){
            doSubClassClick(adapter,view,position);
        }
        if (adapter instanceof HotGoodsAdapter)
        {
            doGoodsClick(adapter,view,position);
        }
    }

    private  void doGoodsClick(BaseQuickAdapter adapter, View view, int position){
        Goods.DataBean bean= (Goods.DataBean) adapter.getData().get(position);
        if (view.getId()== R.id.text_like){
            doLike(bean.getId());
            return;
        }
        Bundle b=new Bundle();
        b.putString("id",bean.getId());
        iView.startActivity(GoodsDetail.class,b,0x234);
        bean=null;


    }


    private void doLike(String id){
        BaseRequest request=new BaseRequest();
        request.setId(id);
        iMode=new ModelLike();
        iMode.getData(request,this);
    }

    private  void doSubClassClick(BaseQuickAdapter adapter, View view, int position){
        iView.startActivity(SearchAct.class);
    }
}
